/*
 * Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary information
 * of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.web.util;

import java.nio.charset.Charset;

import org.apache.commons.lang3.SerializationException;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * ClassName:JsonRedisSeriaziler <br/>
 * Date: 2015年12月30日 上午11:47:09 <br/>
 * 
 * @author Transsion
 * @version
 * @see
 */
public class JsonRedisSeriaziler {

    public static final String  EMPTY_JSON      = "{}";

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    protected ObjectMapper      objectMapper    = new ObjectMapper();

    public JsonRedisSeriaziler(){
    };

    public String seriazilerAsString(Object object) {
        if (null == object) {
            return EMPTY_JSON;
        }

        try {
            return this.objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new SerializationException("Could not write JSON : " + e.getMessage(), e);
        }
    }

    public <T> T deserializeAsObject(String str, Class<T> clazz) {
        if (null == str || null == clazz) {
            return null;
        }
        try {
            return this.objectMapper.readValue(str, clazz);
        } catch (Exception e) {
            throw new SerializationException("Could not write JSON : " + e.getMessage(), e);
        }
    }

}
